package Application.Tool;

import Application.AppJoint;
import javafx.scene.Parent;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.ColorPicker;
import javafx.scene.input.MouseButton;
import javafx.scene.paint.Color;
import Application.LayerImage.LayerImage;
import javafx.scene.input.MouseEvent;
import javafx.scene.shape.Rectangle;


public class Straw extends Tool {
    private Parent root;
    private static Straw ins = new Straw();

    public Rectangle getFrontColorBox() {
        return frontColorBox;
    }

    public void setFrontColorBox(Rectangle frontColorBox) {
        this.frontColorBox = frontColorBox;
    }

    public ColorPicker getFrontColorPicker() {
        return frontColorPicker;
    }

    public void setFrontColorPicker(ColorPicker fronColorPicker) {
        this.frontColorPicker = fronColorPicker;
    }

    private Rectangle frontColorBox;
    private ColorPicker frontColorPicker;

    public Rectangle getBackColorBox() {
        return backColorBox;
    }

    public void setBackColorBox(Rectangle backColorBox) {
        this.backColorBox = backColorBox;
    }

    public ColorPicker getBackColorPicker() {
        return backColorPicker;
    }

    public void setBackColorPicker(ColorPicker backColorPicker) {
        this.backColorPicker = backColorPicker;
    }

    private Rectangle backColorBox;
    private ColorPicker backColorPicker;


    public static int MODE_SAMPLEDOT = 1;
    public static int MODE_3x3 = 2;
    public static int MODE_5x5 = 3;
    public static int MODE_CROSS = 4;
    public static int MODE_X = 5;

    private int u[] = {0, 0, 1, 0, -1, -1, 1, 1, -1, -2, -1, 0, 1, 2, 2, 2, 2, 2, 1, 0, -1, -2, -2, -2, -2};
    private int v[] = {0, -1, 0, 1, 0, -1, -1, 1, 1, -2, -2, -2, -2, -2, -1, 0, 1, 2, 2, 2, 2, 2, 1, 0, -1};

    private int mode = Straw.MODE_SAMPLEDOT;

    private int getAveragedColor(int var1, int var2, int x, int y, LayerImage img) {
        int w = AppJoint.getInstance().getCanvasWidth();
        int h = AppJoint.getInstance().getCanvasHeight();

        int sr = 0;
        int sg = 0;
        int sb = 0;

        int count = 0;

        for(int i = var1; i <= var2; i++) {
            int nx = x + u[i];
            int ny = y + v[i];

            if(nx >= 0 && nx < w && ny >= 0 && ny < h) {
                count++;
                int color = img.getPixelArgb(nx, ny);
                sr += ((color & 0x00ff0000) >> 16);
                sg += ((color & 0x0000ff00) >> 8);
                sb += (color & 0x000000ff);
            }
        }

        int r = sr / count;
        int g = sg / count;
        int b = sb / count;

        return (255 << 24) | (r << 16) | (g << 8) | b;
    }

    private Straw() {
        super();

        // 加载本工具的工具设置
        try {
            root = FXMLLoader.load(getClass().getResource("/View/StrawSettings.fxml"));
//            root = FXMLLoader.load(getClass().getResource("Application/View/StrawSettings.fxml"));
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public void setMode(int mode) {
        this.mode = mode;
    }

    @Override public void onMouseDown(MouseEvent e) {
        int ans = 0;

        LayerImage img = AppJoint.getInstance().getOutImg();

        int x = (int)e.getX();
        int y = (int)e.getY();
        int w = img.getWidth();
        int h = img.getHeight();

        if(mode == MODE_SAMPLEDOT) {
            ans = img.getPixelArgb(x, y);
        } else if(mode == MODE_3x3) {
            ans = this.getAveragedColor(0, 8, x, y, img);
        } else if(mode == MODE_5x5) {
            ans = this.getAveragedColor(0, 24, x, y, img);
        } else if(mode == MODE_CROSS) {
            ans = this.getAveragedColor(0, 4, x, y, img);
        } else if(mode == MODE_X) {
            ans = this.getAveragedColor(5, 8, x, y, img);
        }

        int r = (ans >> 16) & (0xff);
        int g = (ans >> 8) & (0xff);
        int b = (ans) & (0xff);

        if(e.getButton() == MouseButton.PRIMARY) {
            AppJoint.getInstance().setFrontColorArgb(ans);
            frontColorPicker.setValue(Color.rgb(r,g,b));
            frontColorBox.setFill(Color.rgb(r,g,b));
        } else if(e.getButton() == MouseButton.SECONDARY) {
            AppJoint.getInstance().setBackColorArgb(ans);
            backColorPicker.setValue(Color.rgb(r,g,b));
            backColorBox.setFill(Color.rgb(r,g,b));
        }
    }

    @Override public void onMouseMove(MouseEvent e) {

    }

    @Override public void onMouseUp(MouseEvent e) {

    }

    @Override public Parent getToolSettings() {
        return root;
    }

    public static Straw getInstance() {
        return ins;
    }
}
